<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>纯色背景</title>
    <style>
      h1 {
        margin: 0;
      }
      body {
        margin: 0;
        padding: 0;
        height: 100vh;
        background-color: coral;
      }

      .app {
        height: 100vh;
      }

      .toolbar {
        height: 10vh;
      }

      .container {
        height: 90vh;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .viewport {
        position: relative;
        overflow: hidden;
        height: 50%;
        width: 80%;
        border: solid 1px greenyellow;
        margin: 0;
      }

      .slides {
        width: 960px;
        height: 640px;
        position: absolute;
        /* background-color: blueviolet; */
      }
    </style>
    <script>
      window.onload = function () {
        window.addEventListener("resize", onResize);
        onResize();
      };

      function onResize(e) {
        let arr = Array.from(document.querySelectorAll(".slides"));
        const size = getComputedSlideSize();
        const scale = Math.min(
          size.presentationWidth / size.width,
          size.presentationHeight / size.height
        );

        arr.forEach((el) => {
          el.style.width = `${size.width}px`;
          el.style.height = `${size.height}px`;
          if (scale !== 1) {
            if (scale > 1 && window.devicePixelRatio < 2) {
              el.style.zoom = scale;
              el.style.left = "";
              el.style.right = "";
              el.style.top = "";
              el.style.bottom = "";
            } else {
              el.style.zoom = "";
              el.style.left = "50%";
              el.style.top = "50%";
              el.style.bottom = "auto";
              el.style.right = "auto";
              el.style.transform = "translate(-50%, -50%) scale(" + scale + ")";
            }
          }
        });
      }

      function getComputedSlideSize(presentationWidth, presentationHeight) {
        const wrapper = document.querySelector(".viewport");
        const size = {
          width: 960,
          height: 640,
          presentationWidth: presentationWidth || wrapper.offsetWidth,
          presentationHeight: presentationHeight || wrapper.offsetHeight,
        };

        return size;
      }

      function confirmColor() {
        let el = document.querySelector(".slides section");
        let val = document.querySelector("input").value;
        const color = hex2rgb(val);
        el.style.backgroundColor = `rgba(${color[0]}, ${color[1]}, ${color[2]}, 1)`;
      }

      function hex2rgb(hex) {
        let x = [];
        hex = hex.replace("#", "");
        x.push(parseInt(hex.slice(0, 2), 16));
        x.push(parseInt(hex.slice(2, 4), 16));
        x.push(parseInt(hex.slice(4, 6), 16));
        return x;
      }
    </script>
  </head>
  <body>
    <div class="app">
      <div class="toolbar">
        <span>选择纯色背景色</span><input type="color" name="color" onchange="confirmColor()" />
      </div>
      <div class="container">
        <div class="viewport">
          <div class="slides">
            <section style="display: block; width: 100%; height: 100%; position: relative">
              <h1 style="top: 150px; left: 200px; position: absolute">hello world</h1>
            </section>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
